package com.ndp.fb.walle.controller;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.walle.model.vo.OutputResult;
import com.ndp.fb.walle.util.ResponseUtils;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mikey.zhaopeng on 2014/11/17 11:26.
 * Email mikey.zhao@ndpmedia.com
 */
@RequestMapping("/common")
@Controller
public class CommonController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 返回当前时间服务器时间
     * @return
     */
    @RequestMapping(value = "timestamp")
    @ResponseBody
    public ResponseEntity<String> timestamp() {
        ErrorType errorType = null;
        Map data = null;

        try {
            data = new HashMap();
            data.put("date", new Date().getTime());

        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            logger.error(e.getMessage(), e);
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }

    /**
     * 长连接
     * @return
     */
    @RequestMapping(value = "getConfigInfo")
    @ResponseBody
    public ResponseEntity<String> getConfigInfo() {
        ErrorType errorType = null;
        Map data = new HashMap();
        //可以加以改进，根据参数判断获取那个key值
        String heartTime = ConfigCentre.getString(ConfigConst.WEBSOCKET_HEARTBEAT);
        String url = ConfigCentre.getString(ConfigConst.WEBSOCKET_URL);
        data.put("heartTime",heartTime);
        data.put("url",url);
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }

}